package com.ruoyi.riskrule.service.impl;

import com.ruoyi.bizsys.domain.AppUser;
import com.ruoyi.bizsys.domain.LoanIou;
import com.ruoyi.bizsys.domain.LoanOrder;
import com.ruoyi.bizsys.domain.RepaymentOrder;
import com.ruoyi.riskrule.mapper.LoanFieldMapper;
import com.ruoyi.riskrule.service.ILoanFieldService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;

/**
 * @Author: Jamnsin
 * @Date: 2021/3/1 10:43 上午
 */
@Service
public class LoanFieldServiceImpl implements ILoanFieldService {

    @Autowired
    LoanFieldMapper businessMapper;

	@Override
	public List<AppUser> getAppUserByDeviceInfo(String userId) {
		return businessMapper.getAppUserByDeviceInfo(userId);
	}

    @Override
    public LoanOrder getApprovedRecentResult(String custNo) {
        return businessMapper.getApprovedRecentResult(custNo);
    }

    @Override
    public int selectContractsByStatus(String userId, Integer[] status, Date updateTime) {
        return businessMapper.selectContractsByStatus(userId, status, updateTime);
    }

    @Override
    public LoanOrder getLoanOrderOne(String loanNo) {
        return businessMapper.getLoanOrderOne(loanNo);
    }

    @Override
    public LoanIou getLoanIouOne(String loanNo) {
        return businessMapper.getLoanIouOne(loanNo);
    }

    public static void main(String[] args) {
        Integer a=null;
       int b= a <= 0 ? 0 : a;
        System.out.println(b);
    }
    @Override
    public Integer getCurrentOverdue(String custNo) {
        return businessMapper.getCurrentOverdue(custNo);
    }

    @Override
    public Integer getMaximumOverdue(String custNo) {
        return businessMapper.getMaximumOverdue(custNo);
    }

    @Override
    public Integer getLastTimeInterval(String custmerNo) {
        return businessMapper.getLastTimeInterval(custmerNo);
    }

    @Override
    public Integer getLastTimeIntervalOfIou(String custNo) {
        return businessMapper.getLastTimeIntervalOfIou(custNo);
    }

    @Override
    public List<LoanIou> getLoanIouByCustNo(String custNo, Date date) {
        return businessMapper.getLoanIouByCustNo(custNo, date);
    }

    @Override
    public int getFingerprintCustOverdueNum(String fingerprint) {
        return businessMapper.getFingerprintCustOverdueNum(fingerprint);
    }

    @Override
    public int getNewFingerprintCustOverdueNum(String newFingerprint, String table,String deviceType) {
        return businessMapper.getNewFingerprintCustOverdueNum(newFingerprint, table,deviceType);
    }

    @Override
    public BigDecimal withholdingTotal(String custNo) {
        BigDecimal withholdingTotal = businessMapper.withholdingTotal(custNo);
        if (withholdingTotal == null){
            withholdingTotal = BigDecimal.ZERO;
        }
        return withholdingTotal;
    }

    @Override
    public int withholdingSettlementQuantity(String custNo) {
        List<RepaymentOrder> repaymentOrders = businessMapper.withholdingSettlementQuantity(custNo);
        int q = 0;
        Set<String> set = new HashSet<>();
        for (RepaymentOrder e: repaymentOrders) {
            if (set.contains(e.getLoanSerialNo())){
                continue;
            }
            if ("DateBatch".equals(e.getActionType())){
                boolean type = true;
                for (RepaymentOrder repaymentOrder : repaymentOrders) {
                    if (!e.getId().equals(repaymentOrder.getId())
                            && e.getLoanSerialNo().equals(repaymentOrder.getLoanSerialNo()) && !"DateBatch".equals(repaymentOrder.getActionType())){
                        type = false;
                        break;
                    }
                }
                if (type){
                    q++;
                }
                set.add(e.getLoanSerialNo());
            }
        }
        return q;
    }

	@Override
	public Map<String, Integer> getLoanOrderNums(String custNo) {
		return businessMapper.getLoanOrderNums(custNo);
	}



}
